function [predict_lable,accurancy, class_accurancy_rate] = DAGSVM1(train_lable, train_data, test_lable, test_data, type_num, type)
% @train_lable, train_data, data_predict_lable, predic_data type cell
% DAGSVM reference for DAGSVM definition (PartII)
MODEL_NUM = 10;
c_g_1 = [32768, 1.2207e-04; 32, 1.2207e-04; 8192, 1.2207e-04; ...
      32768, 3.0518e-05; 2048, 4.8828e-04; 128, 1.2207e-04;...
      512, 4.8828e-04; 512, 0.0161; 32768, 3.0518e-05;...
      512, 3.0518e-05
];
% c_g_2 = [32768*2,4.8828e-04;
%          2, 0.0020;
%          512, 0.0078;
%          2048, 4.8828e-04;
%          8192, 4.8828e-04;
%          128, 0.0078;
%          8, 0.5000;
%          512, 0.0313;
%          2, 0.0313;
%          2, 0.1250;];



% for  lists = [23    18    31    36    26     2    11    17 ]
% c_g_2 = [32768, 0.0020;
%       2, 0.0020;
%       8192, 1.2207e-04;
%       32768, 1.2207e-04;
%       32, 0.1250;
%       8, 0.0313;
%       8192, 0.0020;
%       8192, 0.0313;
%       8, 0.0313;
%       8, 0.1250 ]

% train
% c_g_2 = [ 8192, 0.0020;
%          2, 0.0020;
%           8192, 4.8828e-04;
%          32768, 4.8828e-04;  
%           8192, 0.0078;
%           8192, 0.0020;
%            32768, 1.2207e-04;
%            32768, 0.0078;
%            32768,  0.0020;
%            32, 0.0020;
%     ]

%  for lists = [23    18    19     9     1    29    24    38    22     8    30    25    37    31    36    26     2    11 17 3 4 5]
% c_g_2 = [32768, 1.2207e-04;
%       32, 0.0020;
%       8, 0.0078;
%       2048, 0.0020;
%       8, 0.1250;
%       32768, 3.0518e-05;
%       512, 0.0078;
%       32, 0.1250;
%       32768, 4.8828e-04;
%       2, 0.1250 ]


%  for lists = [23    18    19     9     1    29    24    38    22     8    30    25    37     6     7     3    21    12  5    15    31    36    26     2    11    17]
c_g_2 = [32768, 1.2207e-04;
        8, 0.0020;
        2048, 1.2207e-04;
        2048, 4.8828e-04;
        2048, 4.8828e-04;
        32, 0.0078;
        8192, 0.0020;
        8, 0.1250;
        32768, 1.2207e-04;
        8, 0.0078;
    ]
if (type == 2)
    c_g = c_g_2;
else
    c_g = c_g_1;
end

model_cell = cell(1,MODEL_NUM);
k = 0;

% 1: 1 - 5
% 2: 1 - 4
% 3: 1 - 3
% 4: 1 - 2
% 5: 2 - 5
% 6: 2 - 4
% 7: 2 - 3
% 8: 3 - 5
% 9: 3 - 4
% 10: 4 - 5
for j = 1 : type_num - 1
    for i = type_num : -1 :j + 1
        k = k + 1;
        model_train_data = [train_data{j};train_data{i}];
        label = [train_lable{j};train_lable{i}];    
        [cv_acc_best, best_C,best_gamma] = cross_validate(label, model_train_data)
         model_cell{k} = svmtrain(label, model_train_data,sprintf('-s 0 -c %f -g %f',c_g(k,1),c_g(k,2)));
%        model_cell{k} = svmtrain(label, model_train_data,sprintf('-s 0 -c %f -g %f',best_C,best_gamma));
    end
end

% DAGSVM
total_error_num = 0;
total_right_num = 0;
predict_lable_array = [];
group = [];
total_data_num = 0;
for type = 1 : type_num
    type_test_data = test_data{type};
    type_test_lable = test_lable{type};
    type_predict_lable = [];
    [test_data_length, height]= size(type_test_data);
    i=1;
    group1 = [];
    group2 = [];
    group3 = [];
    group4 = [];
    error_num = 0;
    right_num = 0;
    
    for data = 1 : test_data_length
        total_data_num = total_data_num + 1;
        group1(i) = svmpredict([type_test_lable(i)], type_test_data(data,:), model_cell{2}); % 1 -- 4
        % not 4
        if (group1(i) == 1)
            group2(i) = svmpredict([type_test_lable(i)], type_test_data(data,:), model_cell{1}); % 1 -- 5
            % not 3
            if (group2(i) == 1)
                group3(i) = svmpredict([type_test_lable(i)], type_test_data(data,:), model_cell{3}); % 1 -- 3
                % not 3
                if (group3(i) == 1)
                    group4(i) = svmpredict([type_test_lable(i)], type_test_data(data,:), model_cell{4}); % 1 -2
                    if (group4(i) == 1)
                        data_predict_lable = 1;
                    else
                        data_predict_lable = 2;
                    end
                % not 1
                else
                    group4(i) = svmpredict([type_test_lable(i)], type_test_data(data,:), model_cell{7}); % 2 -3
                    if (group4(i) == 2)
                        data_predict_lable = 2;
                    else
                        data_predict_lable = 3;
                    end
                end
            % not 1 
            else
                group3(i) = svmpredict([type_test_lable(i)], type_test_data(data,:), model_cell{8}); % 3 -- 5
                % not 3
                if (group3(i) == 5)
                    group4(i) = svmpredict([type_test_lable(i)], type_test_data(data,:), model_cell{5}); % 2 -- 5
                    if (group4(i) == 2)
                        data_predict_lable = 2;
                    else
                        data_predict_lable = 5;
                    end
                % not 5
                else
                    group4(i) = svmpredict([type_test_lable(i)], type_test_data(data,:), model_cell{7}); % 2 -- 3  
                    if (group4(i) == 3)
                        data_predict_lable = 3;
                    else
                        data_predict_lable = 2;
                    end
                end
            end
        % not 1
        else
            group2(i) = svmpredict([type_test_lable(i)], type_test_data(data,:), model_cell{10}); % 4 -- 5
            % not 5
            if (group2(i) == 4)
               group3(i) = svmpredict([type_test_lable(i)], type_test_data(data,:), model_cell{9}); % 3 -- 4
                % not 4
                if (group3(i) == 3)
                    group4(i) = svmpredict([type_test_lable(i)], type_test_data(data,:), model_cell{7}); % 2 -- 3
                    if (group4(i) == 2)
                        data_predict_lable = 2;
                    else
                        data_predict_lable = 3;
                    end
                % not 3
                else
                    group4(i) = svmpredict([type_test_lable(i)], type_test_data(data,:), model_cell{6}); % 2 -- 4  
                    if (group4(i) == 2)
                        data_predict_lable = 2;
                    else
                        data_predict_lable = 4;
                    end
                end
            % not 4 
            else
                group3(i) = svmpredict([type_test_lable(i)], type_test_data(data,:), model_cell{8}); % 3 -- 5
                % not 5
                if (group3(i) == 3)
                    group4(i) = svmpredict([type_test_lable(i)], type_test_data(data,:), model_cell{7}); % 2 -- 3
                    if (group4(i) == 3)
                        data_predict_lable = 3;
                    else
                        data_predict_lable = 2;
                    end
                % not 3
                else
                    group4(i) = svmpredict([type_test_lable(i)], type_test_data(data,:), model_cell{5}); % 2 -- 5  
                    if (group4(i) == 2)
                        data_predict_lable = 2;
                    else
                        data_predict_lable = 5;
                    end
                end
            end
        end
        if (data_predict_lable == type_test_lable(data))
            right_num = right_num + 1;
        else
            error_num = error_num + 1;
        end
        i=i+1;
        type_predict_lable = [type_predict_lable; data_predict_lable];
    % end of data
    end
    class_accurancy_rate{type} = right_num /(error_num + right_num);
    total_right_num = total_right_num + right_num;
    total_error_num = total_error_num + error_num;
    predict_lable_array = [predict_lable_array; type_predict_lable];
    trace = [group1' group2' group3' group4' type_predict_lable type_test_lable];
    group = [group; trace];
% end of type
end
accurancy = total_right_num /(total_error_num + total_right_num);
predict_lable = predict_lable_array;
group
class_accurancy_rate = [class_accurancy_rate{1}(1) class_accurancy_rate{2}(1) class_accurancy_rate{3}(1) class_accurancy_rate{4}(1) class_accurancy_rate{5}(1)];
accurancy
% each class error rate

%group;
end